<?php
echo "关闭页面";
die;
/*
 * 抓取页面数据
 */
require "vendor/jaeger/phpquery-single/phpQuery.php";
require "vendor/jaeger/querylist/QueryList.php";
use QL\QueryList;

//不设置失效时间
set_time_limit(0);

//  获取文章列表链接
$list = [];
for($i = 1;$i <= 70;$i++){
    $hj = QueryList::Query('https://hrb.58.com/baojie/pn'.$i,
        array(
            "title"=>array('.seller a','text')
        ));

    $res = $hj->getData(function($x){
        return $x['title'];
    });
    if(!empty($res)){
        foreach($res as $v){
            array_push($list,$v);
        }
    }
}
print_r($list);
echo count($list);
$data = array_unique($list);
echo count($data);

//foreach($list as $k=>$v){
//    if(explode('/',$v['url'])[3] == 'Blockchain_lemon'){
//        $arr[] = $v;
//    }
//}
////  爬取规则
//$rules = array(
//    "title"=>array('.title-article','text'),
//    "time"=>array('.time','text'),
//    "typename"=>array('.follow-nickName','text'),
//    "read-count"=>array('.read-count','text'),
//    "content"=>array('#js_content','html')
//);
//$data = [];
//foreach($arr as $k => $v){
//    $res = QueryList::Query($v['url'],$rules);
//    $data[$k] = $res->getData(function($x){
//        return $x;
//    })[0];
//}


//  录入库
header("Content-type:text/html;charset=utf-8");
// 创建连接
$conn=mysqli_connect('localhost','root','','loner');//三个参数分别对应服务器名，账号，密码
mysqli_query($conn,'set names utf8');
mysqli_query($conn,'set character_set_client=utf8');
mysqli_query($conn,'set character_set_results=utf');

// 检测连接
if (!$conn) {
    die("连接服务器失败: " . mysqli_connect_error());//连接服务器失败退出程序
}

foreach($data as $k => $v){
    $name = $v;
    mysqli_query($conn,"insert into jiazheng(name,type) values ('$name','baojie')");
}

print_r(mysqli_error($conn));
$res1=mysqli_affected_rows($conn);//返回影响行
if($res1>0){
    echo "添加数据成功</br>";
}else {
    echo "添加数据失败</br>";
}
print_r($res1);



//function escape_str($str,$clear_xss=true,$conn){
//    if(get_magic_quotes_gpc()==0){
//        $str = mysqli_real_escape_string($conn,$str);
//    }
//    if($clear_xss){
//        $str = remove_xss($str);//xss过滤
//    }
//    return $str;
//}
//
//function remove_xss($string) {
//    $string = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S','',$string);
//    $parm1 = array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'title', 'base');
//    $parm2 = array('onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'onchange', 'onclick', 'oncontextmenu', 'oncontrolselect', 'oncopy', 'oncut', 'ondataavailable', 'ondatasetchanged', 'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdate', 'onfilterchange', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlosecapture', 'onmousedown', 'onmouseenter', 'onmouseleave', 'onmousemove', 'onmouseout', 'onmouseover', 'onmouseup', 'onmousewheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 'onpropertychange', 'onreadystatechange', 'onreset', 'onresize', 'onresizeend', 'onresizestart', 'onrowenter', 'onrowexit', 'onrowsdelete', 'onrowsinserted', 'onscroll', 'onselect', 'onselectionchange', 'onselectstart', 'onstart', 'onstop', 'onsubmit', 'onunload');
//    $parm = array_merge($parm1, $parm2);
//    for ($i = 0; $i < sizeof($parm); $i++) {
//        $pattern = '/';
//        for ($j = 0; $j < strlen($parm[$i]); $j++) {
//            if ($j > 0) {
//                $pattern .= '(';
//                $pattern .= '(&#[x|X]0([9][a][b]);?)?';
//                $pattern .= '|(&#0([9][10][13]);?)?';
//                $pattern .= ')?';
//            }
//            $pattern .= $parm[$i][$j];
//        }
//        $pattern .= '/i';
//        $string = preg_replace($pattern, '', $string);
//    }
//    return $string;
//}